package com.house.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RedisTemplateUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public Boolean setString(String key, String val){
        redisTemplate.opsForValue().set(key, val);
        return true;
    }

    public <T> List<T> getStringToList(String key, Class<T> clazz) {
        String s = (String) redisTemplate.opsForValue().get(key);
        if (s == null) {
            return Collections.emptyList();
        }
        return JSON.parseArray(s, clazz);
    }

    /**
     * 带有有效期（分钟）的
     * @param key
     * @param val
     * @param time
     * @return
     */
    public Boolean setString(String key, String val, int time){
        redisTemplate.opsForValue().set(key, val, time, TimeUnit.MINUTES);
        return true;
    }
}
